/**
 * 获取城市列表
 * @type {{getData: cityRequest.getData}}
 */
var cityRequest = {
    getData: function (pid, cid, callback) {
        $.ajax({
            type: "post",
            url: "/Publish/getCityList",
            data: {pid: pid, cid: cid},
            dataType: "json",
            async: false,
            success: function (result) {
                if(result.code == 1){
                    result.pid = pid;
                    result.cid = cid;
                    callback(result);
                }
            }
        });
    }
};

/**
 *
 * @type {{getData: organizerRequest.getData}}
 */
var organizerRequest = {
    getData: function(callback){
        $.ajax({
            type: "post",
            url: "/Publish/getUserOrganizer",
            data: {},
            dataType: "json",
            success: function (result) {
                callback(result);
            }
        });
    },
    adminGetData: function (user_id, organizer_name, callback) {
        $.ajax({
            type: "post",
            url: "/Publish/getUserOrganizer",
            data: {user_id: user_id, organizer_name: organizer_name},
            dataType: "json",
            success: function (result) {
                callback(result);
            }
        });
    }
};

/**
 *
 * @type {{getTypeList: posterRequest.getTypeList, getPicList: posterRequest.getPicList}}
 */
var posterRequest = {
    /**
     * 海报分类列表
     * @param callback
     */
    getTypeList:function(callback){
        $.ajax({
            type:"post",
            url:"/Publish/getPosterTypeList",
            dataType:"json",
            async: false,//异步影响了第一次加载图片
            success:function (result) {
                callback(result);
            }
        });
    },
    /**
     * 海报图片列表
     * @param id
     * @param callback
     */
    getPicList:function(id, callback){
        $.ajax({
            type:"post",
            url:"/Publish/getPosterList",
            data:{id:id},
            dataType:'json',
            async: true,
            success:function (result) {
                result.id = id;
                callback(result);
            }
        });
    }
};

var editRequest ={
    /**
     * 根据标签类别获取指定素材列表
     * @param {type} option
     * @param {type} callback
     * @returns {undefined}
     */
    getTagMaterials:function(option,callback){
        $.ajax({
            type: "POST",
            url: "/Studio/getMaterials",
            async:true,
            data: "tag=" + option.tag + "&page=" + option.page,
            success: function (data) {
                option.data = data;
                if(typeof callback.func === 'function'){
                    callback.func.call(callback.obj || window,option);
                }
            }
        });            
    },
    /**
     * 顶部快捷操作组件
     * @returns {undefined}
     */
    getFastComs:function(option,callback){
        $.ajax({
            type:"POST",
            url:"/Studio/getFastComs",
            dataType:"json",
            async:true,
            success:function(res){
                if(typeof callback.func === 'function'){
                    callback.func.call(callback.obj || window,res.data);
                }                
            }
        });
    },
    /**
     * 我的图库列表
     * @returns {undefined}
     */
    userImageList:function(option,callback){
        $.ajax({
            type:"POST",
            url:"/Studio/userImageList",
            data:{page:option.page,rows:option.rows||10,from:option.from || "left"},
            async:true,
            success:function(res){
                option.data = res;
                if(typeof callback.func === 'function'){
                    callback.func.call(callback.obj || window,option);
                }                
            }
        });
    },    
    addUserImage:function(option,callback){
        $.ajax({
            type:"POST",
            url:"/Studio/addUserImage",
            dataType:"json",
            data:{key:option.key},
            async:false,
            success:function(res){
                try{
                    if(res.data){
                        if(typeof callback.func === 'function'){
                            callback.params.id = res.data;
                            callback.func.call(callback.obj || window,callback.params);
                        }
                    }else{
                        alert("上传图片失败");
                    }
                }catch(err){
                    console.log("add user image exception.");
                }
            }
        });
    },
    delUserImage:function(option,callback){
        $.ajax({
            type:"POST",
            url:"/Studio/delUserImage",
            dataType:"json",
            data:{key:option.key,id:option.id},
            async:true,
            success:function(res){
                try{
                    if(res.code){
                        if(typeof callback.func === 'function'){
                            callback.func.call(callback.obj || window,res.msg);
                        }          
                    }else{
                        alert("删除图片失败");
                    }
                }catch(err){
                    console.log("del my image exception.");
                }
            }
        });
    },
    collectComs:function(option,callback){
        $.ajax({
            type:"POST",
            url:"/Studio/collectComs",
            dataType:"json",
            data:{from:option.from,content:option.content,name:option.name||"",type:option.type},
            async:true,
            success:function(res){
                option.res = res;
                try{
                    if(typeof callback.func === 'function'){
                        callback.func.call(callback.obj || window,option);
                    }                                
                }catch(err){
                    console.log("collect coms exception.");
                }
            }
        });        
    },
    /**
     * 我的素材列表
     * @returns {undefined}
     */
    userMaterialList:function(option,callback){
        $.ajax({
            type:"POST",
            url:"/Studio/userMaterialList",
            data:{page:option.page},
            dataType:"json",
            async:true,
            success:function(res){
                option.data = res.data;
                if(typeof callback.func === 'function'){
                    callback.func.call(callback.obj || window,option);
                }                
            }
        });
    },
    delUserMaterial:function(option,callback){
        $.ajax({
            type:"POST",
            url:"/Studio/delUserMaterial",
            dataType:"json",
            data:{id:option.id},
            async:true,
            success:function(res){
                try{
                    if(res.code === 1){
                        if(typeof callback.func === 'function'){
                            callback.func.call(callback.obj || window,res.data);
                        }                                
                    }
                }catch(err){
                    console.log("del my material exception.");
                }
            }
        });        
    },
    getActivityInfo:function(option,callback){
        $.ajax({
            type:"POST",
            url:"/Studio/getActivityInfo",
            dataType:"json",
            data:option,
            async:true,
            success:function(res){
                try{
                    if(typeof callback.func === 'function'){
                        callback.func.call(callback.obj || window,res);
                    }                                
                }catch(err){
                    console.log("get activity info exception.");
                }
            }
        });         
    },
    getTemplateInfo:function(option,callback){
        $.ajax({
            type:"POST",
            url:"/Studio/templateDetail",
            dataType:"json",
            data:option,
            async:true,
            success:function(res){
                try{
                    if(typeof callback.func === 'function'){
                        callback.func.call(callback.obj || window,res);
                    }                                
                }catch(err){
                    console.log("get template info exception.");
                }
            }
        });         
    },    
    guideEnd:function(option,callback){
        $.ajax({
            type:"POST",
            url:"/Studio/guideEnd",
            dataType:"json",
            data:{},
            async:true,
            success:function(res){
                try{
                    if(typeof callback.func === 'function'){
                        callback.func.call(callback.obj || window,res);
                    }                                
                }catch(err){
                    console.log("guideEnd  exception.");
                }
            }
        });         
    },
    catcheImg:function(option,callback){
        $.ajax({
            type:"POST",
            url:"/Studio/catchImage",
            dataType:"json",
            data:{imgs:option.url},
            async:true,
            success:function(res){
                try{
                    if(typeof callback.func === 'function'){
//                        console.log(res['data'][0]['url']);
                        callback.params.url = res['data'][0]['url'];
                        callback.func.call(callback.obj || window,callback.params);
                    }                                
                }catch(err){
                    console.log("catcheImg  exception.");
                }
            }
        });         
    },
    catcheImg2:function(option,callback){
        $.ajax({
            type:"POST",
            url:"/Studio/catchImage",
            dataType:"json",
            data:{imgs:option.url},
            async:true,
            success:function (result) {
                callback(result);
            }
        });
    },    
    //获取模板标签分组信息
    getTemplateTagsGroup : function(data, callback){
        $.ajax({
            type:"POST",
            url:"/TemplateTags/getTagsGroup",
            async:true,
            dataType:"json",
            data:data,
            success:function(res){
                callback(res);
            }
        });
    },
    //获取模板详情内容 用以渲染或编辑活动
    templateDetail : function(data, callback){
        $.ajax({
            type:"POST",
            url:"/Studio/templateDetail",
            async:true,
            dataType:"json",
            data:data,
            success:function(res){
                callback(res);
            }
        });
    },  
    //获取模板信息 用以编辑模板
    getEditTemplate : function(data, callback){
        $.ajax({
            type:"POST",
            url:"/Studio/getEditTemplate",
            async:true,
            dataType:"json",
            data:data,
            success:function(res){
                callback(res);
            }
        });
    }      
    
    
};


var signTypeRequest = {
    getData : function(callback){
        $.ajax({
            type:"POST",
            url:"/Publish/getSignType",
            dataType:"json",
            async:true,
            success:function(res){
                callback(res);
            }
        });
    }
};

var publishRequest = {
    publish : function(data, callback){
        $.ajax({
            type:"POST",
            url:"/Publish/publishActivity",
            async:false,//异步回调弹窗会被认为非用户操作
            dataType:"json",
            data:data,
            success:function(res){
                callback(res);
            }
        });
    },
    //保存素材模板
    toSaveTemplate : function(data, callback){
        $.ajax({
            type:"POST",
            url:"/Template/toSave",
            async:false,//异步回调弹窗会被认为非用户操作
            dataType:"json",
            data:data,
            success:function(res){
                callback(res);
            }
        });
    },
    //保存素材模板预览
    toSaveTemplatePreview : function(data, callback){
        $.ajax({
            type:"POST",
            url:"/Template/toPreview",
            async:false,//异步回调弹窗会被认为非用户操作
            dataType:"json",
            data:data,
            success:function(res){
                callback(res);
            }
        });
    }     
    
};